package demo2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-11-02
 * Time:20:40
 */
/*
反射的优缺点
 */
public class ReflectClassDemo {
    public static void reflectPrivateConstructor(){
        Class<?> c1 = null;
        try {
            c1 = Class.forName("demo2.Student");
           Constructor<?> constructor =  c1.getDeclaredConstructor(String.class,int.class);

           constructor.setAccessible(true);
            Student student = (Student)constructor.newInstance("gaobo",18);
            System.out.println(student);
        }catch (ClassNotFoundException | NoSuchMethodException e){
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    //通过反射获取对象
    public static void reflectNewInstance(){
        Class<?> c1 = null;
        try {
            c1 = Class.forName("demo2.Student");

            //调用不带参数的构造方法
            Student student = (Student) c1.newInstance();//
            System.out.println(student);
            student.sleep();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    //反射获取属性，直接获取的是私有的属性
    public static void reflectPrivateField(){
        Class<?> c1 = null;
        try {
            c1 = Class.forName("demo2.Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student) c1.newInstance();

            //修改student这个字段，把值修改为”GAOBO“
            field.set(student,"gaobo");
            String str = (String) field.get(student);
            System.out.println(str);

        } catch (ClassNotFoundException | NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateMethod(){
        Class<?> c1 = null;
        try {
            c1 = Class.forName("demo2.Student");
            Method method = c1.getDeclaredMethod("function", String.class);
            Student student =(Student) c1.newInstance();
            method.setAccessible(true);
            method.invoke(student,"我是student对象的私有方法的参数");


        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
//        reflectNewInstance();
//        reflectPrivateConstructor();
//        reflectPrivateField();
    reflectPrivateMethod();
    }

}
//java当中创建对象的方法
/*
1.new
2.clone
3.newInstance 非常的不安全
 */

